package org.snail.employee.service.listener;

import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j;
import org.snail.constant.GroupConstant;
import org.snail.dept.bean.DepartDO;
import org.snail.employee.service.impl.EmployeesOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;


/**
 * 钉钉人员API调用对列
 */
@Component("employeesListener")
@Log4j
public class EmployeesListener implements MessageListener {

    @Autowired
    private EmployeesOptionService employeesOptionService;

    @Override
    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            log.info("=======================进行activemq  同步部门人员信息：" + textMessage);
            String text = textMessage.getText();
            DepartDO departDO = JSON.parseObject(text, DepartDO.class);
            log.info("`````````````部门人员:departDO:"+departDO);

            if (null == departDO) {
                log.error("activemq中，json转换异常！");
            } else {
                employeesOptionService.synchronizeUserByDepartment(departDO);
            }
        } catch (JMSException e) {
            log.error("钉钉接口同步部门人员信息  activemq错误!", e);
        } catch (Exception e) {
            log.error(e);
        }
    }
}
